﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    public class ApiArticle: ApiBase
    {
        public ApiArticle(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者
        string req_author { get { return GetStringParam(new ReqStringParamOption{ Name = "author", Level= REQPARAMLEVEL.FREE }); } }

        #endregion

        public void Do(string action)
        {
            switch (action)
            {
                case "get":
                    Article get_article = G.DB.GetById<Article>(req_id);
                    ResponseJson(new ObjectResult { data = get_article } );
                    break;
                case "status":
                    CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.ARTICLE).Modify());
                    // 设置状态
                    int status_id = req_id;
                    int status = req_status;
                    ResponseJson(new Result());
                    break;
                case "add":
                    CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.ARTICLE).Add());
                    // 添加操作
                    Article add_article = new Article { author = req_author, create_time = DateTimeEx.GetNowTimeStamp() };
                    if (G.DB.Insert<Article>(add_article) == 0) ThrowStatus(STATUS.FAILED_INSERT);
                    else ResponseJson(new IDResult { id = add_article.id });
                    break;
                case "modify":
                    CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.ARTICLE).Modify());
                    // 修改操作
                    Article modify_article = G.DB.GetById<Article>(req_id);
                    modify_article.author = req_author;
                    if (G.DB.Update<Article>(modify_article) == 0) ThrowStatus(STATUS.FAILED_UPDATE);
                    else ResponseJson(new Result());
                    break;
                case "delete":
                    CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.ARTICLE).Delete());
                    // 删除操作
                    if (G.DB.Delete<Article>(req_id) == 0) ThrowStatus(STATUS.FAILED_DELETE);
                    else ResponseJson(new Result());
                    break;
                default: break;
            }
        }
    }


}